package com.java47.controller;

import com.java47.pojo.Item;
import com.java47.pojo.Option;
import com.java47.pojo.Subject;
import com.java47.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VoteController {

    @Autowired
    private UserService us;

    @RequestMapping("toVote")
    public String toVote(Model model,String sid){
        Subject s = us.showOne(sid);
        List<Option> olist = us.showOlist(sid);
        int num = 0;
        for(Option o :olist){
            num += Integer.parseInt(o.getOrder());
        }
        s.setSum(num);
        s.setJs(olist.size());
        model.addAttribute("subject",s);
        model.addAttribute("olist",olist);
        return "vote";
    }

    @RequestMapping("toVoteview")
    private String toVoteview(Model model,String sid){
        Subject s = us.showOne(sid);
        List<Option> olist = us.showOlist(sid);
        int num = 0;
        for(Option o : olist){
            num+=Integer.parseInt(o.getOrder());
        }
        for(int i=0;i<olist.size();i++){
            olist.get(i).setLenth(Double.parseDouble(olist.get(i).getOrder())/num*100);
            System.out.println("***************"+olist.get(i).getOrder());
            System.out.println("==========="+Double.parseDouble(olist.get(i).getOrder())/num*100);
        }
        System.out.println("OOOOO"+olist);
        s.setJs(olist.size());
        s.setSum(num);
        model.addAttribute("num",num);
        model.addAttribute("subject",s);
        model.addAttribute("olist",olist);
        return "voteview";
    }

    @RequestMapping("vote")
    private String vote(String sid, String oid,Model model, HttpSession session){
        boolean b =us.selectItem((String)session.getAttribute("uid"),sid);
        if(b&&oid!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("update",new Item(sid,oid));
            boolean b1 = us.update(map);
            map.put("add",new Item((String)session.getAttribute("uid"),sid,oid));
            boolean b2 =us.update(map);
        }else{
            System.out.println("***************已经投过票 或者 空投*****************");
        }
        return "forward:slist";
    }

    @RequestMapping("addOption")
    private String addOption(String[] options,String title,String str){
        System.out.println("==============先加Subject==============");
        Map<String,Object> smap = new HashMap<>();
        smap.put("addSubject",title);
        us.update(smap);
        System.out.println("==============再查询到Subject表里面的最后个sid==============");
        String sid = "";
        for(Subject s :us.show()){
            sid=s.getSid();
        }
        System.out.println("==============再加Option==============");
        for(int i =0;i<options.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("addOption",new Option(options[i],sid));
            us.update(map);
        }
        return "redirect:slist";
    }
}
